package org.mtmi.ui.scenes;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.mtmi.ui.GraphicUtil;
import org.mtmi.ui.M;
import org.mtmi.ui.actions.IAction;
import org.mtmi.ui.controls.Button;
import org.mtmi.ui.controls.Control;
import org.mtmi.ui.controls.IContentProvider;
import org.mtmi.ui.controls.ILabelProvider;
import org.mtmi.ui.controls.IVisualControl;
import org.mtmi.ui.controls.ListView;
import org.mtmi.ui.controls.TouchControlEvent;
import org.mtmi.ui.controls.listeners.ControlTappedListener;
import org.mtmi.ui.controls.listeners.ISelectionListener;
import org.mtmi.ui.controls.listeners.SelectionEvent;
import org.mtmi.ui.controls.listeners.TapEvent;
import org.mtmi.ui.controls.popups.Popup;
import org.mtmi.ui.drawables.IDrawable;
import org.mtmi.ui.drawables.ImageDrawable;
import org.mtmi.ui.layouts.LayoutData;
import org.mtmi.ui.scenes.actions.NextSceneAction;
import org.mtmi.ui.scenes.actions.PrevSceneAction;
import org.mtmi.ui.scenes.actions.QuitAction;
import org.mtmi.ui.scenes.actions.ToggleEditSceneModeAction;

/**
 * Scene popup displaying global actions.
 * 
 */
public class GlobalScenePopup extends Popup implements ControlTappedListener
{
	private class LabelProvider implements ILabelProvider
	{
		@Override
		public IDrawable getImage(Object element) {
			if( element instanceof Scene ) {
/*TODO:				String key = ((Scene)element).getIconKey();
				System.out.println(">> scene key="+key);
				if( key!=null ) {
					return IMG.GetD( key );
				}*/
				return new ImageDrawable( ((Scene)element).getMiniature() );
			}
			return null;
		}

		@Override
		public String getText(Object element) {
			//scene have no name...
			return null;
		}
	}
	
	public static class ContentProvider implements IContentProvider
	{
		@Override
		public Object[] getElements( Object input ) {
			return SceneManager.getDefault().getScenes();
		}
	}
	
	private class SelectionListener implements ISelectionListener
	{

		@Override
		public void selectionChanged(SelectionEvent e) {}

		@Override
		public void selectionConfirmed(SelectionEvent e) 
		{
			Scene s = (Scene)e.selection;
			GlobalScenePopup.this.dispose();
			SceneManager.getDefault().setScene( s );
			redraw();
		}
		
	}
	
	private Button btn_next;
	private Button btn_prev;
	private Button btn_edit_or_play;
	private Button btn_quit;
	private ListView lv_scenes;
	
	private Rectangle contents;
	
	private GlobalSceneButton gsb;
	
	public GlobalScenePopup( Scene scene, int flags ) {
		super( scene, flags|M.SCENE_EDITOR_CONTROL );
		
		btn_next = createButton( new NextSceneAction() );
		btn_prev = createButton( new PrevSceneAction() );
		btn_edit_or_play = createButton( new ToggleEditSceneModeAction() );
		btn_quit = createButton( new QuitAction() );
		
		lv_scenes = new ListView( scene, M.SCENE_EDITOR_CONTROL );
		lv_scenes.setLabelProvider( new LabelProvider() );
		lv_scenes.setContentProvider( new ContentProvider() );
		lv_scenes.setInput( "" ); //any input is ok. //$NON-NLS-1$
		lv_scenes.addSelectionListener( new SelectionListener() );
		lv_scenes.setBackground( COLOR.grey0 );
		lv_scenes.setBackgroundSelection( COLOR.grey100 );
		Point sz = scene.getSize();
		int aw = sz.x/2; //half screen for list view
		int maxcol = aw / Scene.L_MINIATURE;
		int ncol = SceneManager.getDefault().getSceneCount() / maxcol;
		if( ncol==0 ) ncol=1; else if( ncol > maxcol ) ncol=maxcol;
		lv_scenes.setColumnCount( ncol );
		
		//get buttons sizes
		int wx = 0;
		int hx = 0;
		Point s = btn_next.getSize();
		wx = Math.max( wx, s.x );
		hx = Math.max( hx, s.y );
		s = btn_prev.getSize();
		wx = Math.max( wx, s.x );
		hx = Math.max( hx, s.y );
		s = btn_edit_or_play.getSize();
		wx = Math.max( wx, s.x );
		hx = Math.max( hx, s.y );
		s = btn_quit.getSize();
		wx = Math.max( wx, s.x );
		hx = Math.max( hx, s.y );
		
		//listview size:
		Point lsz = lv_scenes.computeSize( new LayoutData(scene) );
		int lw = Math.max( 100, lsz.x );
		
		//layout buttons
		s = scene.getSize();
		int x = (s.x-wx-20-lw)/2;
		int mg = 10;
		int th = 4*hx + 9*mg;
		int y0 = (s.y-th)/2;
		int y = y0;
		
		
		btn_next.setBounds( x, y, wx, hx );
		y += hx+mg;
		btn_prev.setBounds( x, y, wx, hx );
		y += hx+4*mg;
		btn_edit_or_play.setBounds( x, y, wx, hx );
		y += hx+4*mg;
		btn_quit.setBounds( x, y, wx, hx );
		
		lv_scenes.setBounds( x+wx+20, y0, lw, th );
		
		contents =new Rectangle( x, y0, wx+20+lw, th );
		
		lv_scenes.setSelection( SceneManager.getDefault().getScene() );
		
		gsb = getGSB();
	}
	
	private Button createButton( IAction a ) {
		Button btn = new Button( getScene(), M.SCENE_EDITOR_CONTROL );
		btn.setImageAndText( a.getImage(), a.getText() );
		btn.setEnabled( a.isEnabled() );
		btn.addTapListener( this );
		return btn;
	}

	@Override
	public void dispose() {
		super.dispose();
		btn_next.dispose();
		btn_prev.dispose();
		btn_edit_or_play.dispose();
		btn_quit.dispose();
		lv_scenes.dispose();
	}
	
	private GlobalSceneButton getGSB()
	{
		Control cs[] = getScene().getControls( true );
		for( Control c : cs ) {
			if( c instanceof GlobalSceneButton ) {
				return (GlobalSceneButton)c;
			}
		}
		return null;
	}
	
	@Override
	public boolean containsTouch( TouchControlEvent e ) {
		//behave like gsb is part of popup
		if( gsb.containsTouch( e ) ) {
			return false;
		}
		return super.containsTouch( e );
	}

	@Override
	public void paintContent(GC gc, int width, int height) {
		
		gc.setBackground( COLOR.grey50 );
		int mg = 20;
		GraphicUtil.fillRoundRectangle( gc, contents.x-mg, contents.y-mg, contents.width+2*mg, contents.height+2*mg, 30,30 );
		
		Color s = gsb.getForeground();
		gsb.setForeground( COLOR.grey100 );
		gsb.paint( gc );
		gsb.setForeground( s );
	}

	@Override
	public void controlTappedDown( TapEvent event ) {}

	@Override
	public void controlTappedUp( TapEvent event ) {
		IVisualControl src = event.control;
		if( src == btn_next ) {
			dispose();
			new NextSceneAction().run();
		}
		else if( src == btn_prev ) {
			dispose();
			new PrevSceneAction().run();
		}
		else if( src == btn_edit_or_play ) {
			dispose();
			Scene sel = (Scene)lv_scenes.getSelection();
			if( sel != SceneManager.getDefault().getScene() ) {
				SceneManager.getDefault().setScene( sel );
			}
			new ToggleEditSceneModeAction().run();
		}
		else if( src == btn_quit ) {
			dispose();
			new QuitAction().run();
		}
		else throw new Error("unhandled src: "+src); //$NON-NLS-1$
		
	}
}
